/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on May 1, 2010, 1:17:08 PM
 */

package app.ui;

import app.algo.GaussianScaleSpacePyramidBuilder;
import app.algo.ScaleSpaceExtremaDetector;
import app.algo.SiftSettings;
import app.utils.ImageUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFileChooser;
import app.algo.SiftFeature;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

/**
 *
 * @author bharat
 */
public class MainPanel extends javax.swing.JPanel {

    BufferedImage queryImg, searchImg ;
    int qImgWidth = 352, qImgHeight = 288, sImgWidth = 352, sImgHeight=288 ;
    JPanel imgPanel, resultPanel ;


    /** Creates new form MainPanel */
    public MainPanel() {
        initComponents();
        imgPanel = new JPanel();
        imgPanel.setLayout(new BoxLayout(imgPanel, BoxLayout.Y_AXIS));
        queryImgScrollPane.setViewportView(imgPanel);
        queryImgScrollPane.repaint();

        resultPanel = new JPanel();
        resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.Y_AXIS));
        resultScrollPane.setViewportView(resultPanel);
        resultScrollPane.repaint();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        queryImgBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        QueryImgPath = new javax.swing.JLabel();
        queryImgWidthLabel = new javax.swing.JLabel();
        queryImgWidth = new javax.swing.JTextField();
        queryImgHeightLabel = new javax.swing.JLabel();
        queryImgHeight = new javax.swing.JTextField();
        queryLabel = new javax.swing.JLabel();
        queryImgHeightLabel1 = new javax.swing.JLabel();
        searchImgPath = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        searchImgBtn = new javax.swing.JButton();
        searchImgWidth = new javax.swing.JTextField();
        queryImgWidthLabel1 = new javax.swing.JLabel();
        searchImgHeight = new javax.swing.JTextField();
        queryLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        queryImgScrollPane = new javax.swing.JScrollPane();
        searchBtn = new javax.swing.JButton();
        resetBtn = new javax.swing.JButton();
        exitBtn = new javax.swing.JButton();
        resultScrollPane = new javax.swing.JScrollPane();

        setPreferredSize(new java.awt.Dimension(1024, 600));

        queryImgBtn.setText("Bowse...");
        queryImgBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                queryImgBtnActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel1.setText("Query Image:");

        queryImgWidthLabel.setText("Width:");

        queryImgWidth.setText("352");

        queryImgHeightLabel.setText("Height:");

        queryImgHeight.setText("288");

        queryLabel.setText("Note: Required for raw RGB images.");

        queryImgHeightLabel1.setText("Height:");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel2.setText("Search Image:");

        searchImgBtn.setText("Bowse...");
        searchImgBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchImgBtnActionPerformed(evt);
            }
        });

        searchImgWidth.setText("352");

        queryImgWidthLabel1.setText("Width:");

        searchImgHeight.setText("288");

        queryLabel1.setText("Note: Required for raw RGB images.");

        searchBtn.setText("Search");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        resetBtn.setText("Reset");
        resetBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBtnActionPerformed(evt);
            }
        });

        exitBtn.setText("Exit");
        exitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(queryImgWidthLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(queryImgWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(queryImgHeightLabel)
                                .addGap(18, 18, 18)
                                .addComponent(queryImgHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(queryLabel)
                                .addGap(110, 110, 110)
                                .addComponent(searchBtn)
                                .addGap(18, 18, 18)
                                .addComponent(resetBtn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(exitBtn)
                                .addGap(42, 42, 42))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(searchImgBtn)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(QueryImgPath, javax.swing.GroupLayout.PREFERRED_SIZE, 474, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(queryImgBtn)))
                                .addGap(315, 315, 315))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(queryImgWidthLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchImgWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(queryImgHeightLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(searchImgHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(queryLabel1))
                            .addComponent(searchImgPath, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(340, 340, 340))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(queryImgScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(resultScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(queryImgBtn)
                            .addComponent(QueryImgPath))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(queryImgWidthLabel)
                            .addComponent(queryImgWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(queryImgHeightLabel)
                            .addComponent(queryImgHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(queryLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(searchImgPath)
                            .addComponent(searchImgBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(queryImgWidthLabel1)
                            .addComponent(searchImgWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(queryImgHeightLabel1)
                            .addComponent(searchImgHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(queryLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchBtn)
                            .addComponent(resetBtn)
                            .addComponent(exitBtn))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(resultScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addComponent(queryImgScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void queryImgBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_queryImgBtnActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser(new File("."));
        int retval = fc.showDialog(this, "Select Query Image");

         if (retval == JFileChooser.APPROVE_OPTION) {
            File queryImgFile = fc.getSelectedFile();
            QueryImgPath.setText(queryImgFile.getAbsolutePath());
            try{
                qImgWidth = Integer.parseInt(queryImgWidth.getText()) ;
                qImgHeight = Integer.parseInt(queryImgHeight.getText()) ;

                if (queryImgFile.getAbsolutePath().toUpperCase().endsWith("RGB")) {
                    queryImg = ImageUtils.readRGBImage(queryImgFile, qImgWidth, qImgHeight);
                }else{
                    queryImg = ImageUtils.readImage(queryImgFile) ;
                }
                
                JLabel l = new JLabel(new ImageIcon(queryImg)) ;
                l.setBorder(new TitledBorder("Query Image")) ;

                imgPanel.add(l);
                imgPanel.repaint();

            }catch(Exception e){}
        }
    }//GEN-LAST:event_queryImgBtnActionPerformed

    private void searchImgBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchImgBtnActionPerformed
        JFileChooser fc = new JFileChooser(new File("."));
        int retval = fc.showDialog(this, "Select Search Image");

         if (retval == JFileChooser.APPROVE_OPTION) {
            File searchImgFile = fc.getSelectedFile();
            searchImgPath.setText(searchImgFile.getAbsolutePath());
            try{
                sImgWidth = Integer.parseInt(searchImgWidth.getText()) ;
                sImgHeight = Integer.parseInt(searchImgHeight.getText()) ;

                if (searchImgFile.getAbsolutePath().toUpperCase().endsWith("RGB")) {
                    searchImg = ImageUtils.readRGBImage(searchImgFile, qImgWidth, qImgHeight);
                }else{
                    searchImg = ImageUtils.readImage(searchImgFile) ;
                }

                JLabel l = new JLabel(new ImageIcon(searchImg)) ;
                l.setBorder(new TitledBorder("Search Image")) ;

                imgPanel.add(l);
                imgPanel.repaint();
            }catch(Exception e){}
        }
    }//GEN-LAST:event_searchImgBtnActionPerformed

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed

        BufferedImage img = queryImg;

        GaussianScaleSpacePyramidBuilder gpb = new GaussianScaleSpacePyramidBuilder();
        BufferedImage[][] imgPyramids = gpb.buildGaussianPyramid(img, SiftSettings.SIFT_INTERVALS, SiftSettings.SIFT_SIGMA);

        int octvs = imgPyramids.length ;

        BufferedImage[][] DOGPyramids = new BufferedImage[octvs][];

        for (int o = 0; o < octvs; o++) {
            DOGPyramids[o] = gpb.buildDogPyramid(imgPyramids[o], octvs, SiftSettings.SIFT_INTERVALS);
        }

        JLabel l = new JLabel(new ImageIcon(DOGPyramids[2][0])) ;
        l.setBorder(new TitledBorder("Gauss20")) ;
        resultPanel.add(l);

        l = new JLabel(new ImageIcon(DOGPyramids[2][1])) ;
        l.setBorder(new TitledBorder("Gauss21")) ;
        resultPanel.add(l);

        l = new JLabel(new ImageIcon(DOGPyramids[2][2])) ;
        l.setBorder(new TitledBorder("Gauss22")) ;
        resultPanel.add(l);
        
        resultPanel.repaint();
        resultScrollPane.repaint();
        repaint();

        ScaleSpaceExtremaDetector sced = new ScaleSpaceExtremaDetector();
        ArrayList<SiftFeature> features = sced.detectExtremas(DOGPyramids, octvs, SiftSettings.SIFT_INTERVALS, SiftSettings.SIFT_CONTRAST_THRESOLD, SiftSettings.SIFT_CURVATURE_THRESOLD) ;
    }//GEN-LAST:event_searchBtnActionPerformed

    private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBtnActionPerformed
        imgPanel.removeAll();
        resultPanel.removeAll() ;

        searchImg = null ;
        searchImg = null ;
        QueryImgPath.setText(null);
        searchImgPath.setText(null);
        queryImgScrollPane.repaint();
        resultScrollPane.repaint() ;
        repaint();
    }//GEN-LAST:event_resetBtnActionPerformed

    private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitBtnActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel QueryImgPath;
    private javax.swing.JButton exitBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton queryImgBtn;
    private javax.swing.JTextField queryImgHeight;
    private javax.swing.JLabel queryImgHeightLabel;
    private javax.swing.JLabel queryImgHeightLabel1;
    private javax.swing.JScrollPane queryImgScrollPane;
    private javax.swing.JTextField queryImgWidth;
    private javax.swing.JLabel queryImgWidthLabel;
    private javax.swing.JLabel queryImgWidthLabel1;
    private javax.swing.JLabel queryLabel;
    private javax.swing.JLabel queryLabel1;
    private javax.swing.JButton resetBtn;
    private javax.swing.JScrollPane resultScrollPane;
    private javax.swing.JButton searchBtn;
    private javax.swing.JButton searchImgBtn;
    private javax.swing.JTextField searchImgHeight;
    private javax.swing.JLabel searchImgPath;
    private javax.swing.JTextField searchImgWidth;
    // End of variables declaration//GEN-END:variables

}
